<template>
  <nav class="app-topnav">
    <div class="container">
      <ul>
        <!-- 登录状态和不登录状态的显示 -->
        <template v-if="$store.state.user.profile.token">
          <li><a href="javascript:;"><i class="iconfont icon-user"></i>{{ $store.state.user.profile.account }}</a></li>
          <li><a @click="logout" href="javascript:;">退出登录</a></li>
        </template>
        <template v-else>
          <li>
            <a href="javascript:;" @click="toLogin">请先登录</a>
          </li>
          <li><a href="javascript:;">免费注册</a></li>
        </template>
        <li><RouterLink to="/member/order">我的订单</RouterLink></li>
        <li><RouterLink to="/member">会员中心</RouterLink></li>
        <li><a href="javascript:;">帮助中心</a></li>
        <li><a href="javascript:;">关于我们</a></li>
        <li><a href="javascript:;"><i class="iconfont icon-phone"></i>手机版</a></li>
      </ul>
    </div>
  </nav>
</template>

<script setup>
import { getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
const router = useRouter()
const store = useStore()
const { proxy: app } = getCurrentInstance()
// 去登录
function toLogin() {
  const fullPath = router.currentRoute.value.fullPath
  store.commit('user/setCurrentPath', fullPath)
  router.push('/login')
}
// 退出登录
function logout() {
  store.commit('user/setUser', {})
  // 清空购物车数据
  store.commit('cart/setCartList', [])
  app.$message({ type: 'success', text: '退出成功' })
  router.push('/login')
}
</script>

<style lang="less" scoped>
.app-topnav {
  background: #333;

  ul {
    display: flex;
    height: 53px;
    justify-content: flex-end;
    align-items: center;

    li {
      a {
        padding: 0 15px;
        color: #cdcdcd;
        line-height: 1;
        display: inline-block;

        i {
          font-size: 14px;
          margin-right: 2px;
        }

        &:hover {
          color: @xtxColor;
        }
      }

      ~li {
        a {
          border-left: 2px solid #666;
        }
      }
    }
  }
}
</style>